﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Drawing;
using System.Web.SessionState;
using Maticsoft.Common;

namespace MidSoft.Finance.Contract
{
    public partial class Modify : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputButton btnCancel;
        public string adminname = "管理部门";

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int ContractId = ChinaSoft.Common.Input.SetInt(Request.Params["id"]);
                    BindData(ContractId);
                }
            }
        }
        /// <summary>
        /// 获取合同信息
        /// </summary>
        /// <param name="ProjectInfoId"></param>
        private void BindData(int ContractId)
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("*", "V_ContractList", "ContractId=" + ContractId);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                this.lblProjectName.Text = dr["ProjectName"].ToString();
                this.lblProjectNumber.Text = dr["ProjectNumber"].ToString();
                this.txtContractNumber.Text = dr["ContractNumber"].ToString();
                this.txtContractName.Text = dr["ContractName"].ToString();
                this.txtUnit.Text = dr["Unit"].ToString();
                this.txtContractMoney.Text =dr["ContractMoney"].ToString();
                this.txtConfirmDate.Value =Convert.ToDateTime(dr["ConfirmDate"].ToString()).ToShortDateString();
                this.txtAddress.Text = dr["Address"].ToString();
                this.ddlType.SelectedValue =dr["ContractType"].ToString();
            }
        }


        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            string strErr = "";
            if (this.txtContractNumber.Text.Trim().Length == 0)
            {
                strErr += "合同编号不能为空！\\n";
            }
            if (this.txtContractName.Text.Trim().Length == 0)
            {
                strErr += "合同名称不能为空！\\n";
            }
            if (this.txtUnit.Text.Trim().Length == 0)
            {
                strErr += "签订单位不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtContractMoney.Text))
            {
                strErr += "合同金额格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtConfirmDate.Value))
            {
                strErr += "签订日期格式错误！\\n";
            }
            if (this.txtAddress.Text.Trim().Length == 0)
            {
                strErr += "地址不能为空！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int ContractId=ChinaSoft.Common.Input.SetInt(Request.Params["id"]);
            string ContractNumber = this.txtContractNumber.Text;
            string ContractName = this.txtContractName.Text;
            string Unit = this.txtUnit.Text;
            decimal ContractMoney = decimal.Parse(this.txtContractMoney.Text);
            DateTime ConfirmDate = DateTime.Parse(this.txtConfirmDate.Value);
            string Address = this.txtAddress.Text;
            string FileFolder = "";
            string FileSaveName = "";
            string FileName = "";
            #region 上传文档
            if (FileUpload1.HasFile)
            {
                string result = ChinaSoft.Common.UpFile.UpDocFile(FileUpload1, Server.MapPath("/Uploads"), "", 5);
                if (result.Contains("错误"))
                {
                    Maticsoft.Common.MessageBox.Show(this, result);
                    return;
                }
                else
                {
                    FileFolder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
                    FileSaveName = result;
                    FileName = FileUpload1.FileName;
                }
            }
            #endregion

            string ContractType = this.ddlType.SelectedValue;
            ChinaSoft.BLL.BLL_tb_MidSoft_Contract bll = new ChinaSoft.BLL.BLL_tb_MidSoft_Contract();
            ChinaSoft.Model.Model_tb_MidSoft_Contract model =bll.GetModel(ContractId);
            model.ContractNumber = ContractNumber;
            model.ContractName = ContractName;
            model.Unit = Unit;
            model.ContractMoney = ContractMoney;
            model.ConfirmDate = ConfirmDate;
            model.Address = Address;
            if (FileFolder != "")
            {
                model.FileFolder = FileFolder;
                model.FileSaveName = FileSaveName;
                model.FileName = FileName;
            }

            model.ContractType = ContractType;

            bll.Update(model);
            #region 添加项目日志
            ChinaSoft.Model.Model_tb_Midsoft_ProjectLog modelProjectLog = new ChinaSoft.Model.Model_tb_Midsoft_ProjectLog();
            ChinaSoft.BLL.BLL_tb_Midsoft_ProjectLog bllProjectLog = new ChinaSoft.BLL.BLL_tb_Midsoft_ProjectLog();
            modelProjectLog.CreateTime = DateTime.Now;
            modelProjectLog.CreateUseId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            string logcontent;
            if (ContractType == "0")
                logcontent = "修改主合同“" + ContractName + "”成功！主合同编号：" + ContractNumber;
            else
                logcontent = "修改分包合同“" + ContractName + "”成功！分包合同编号：" + ContractNumber;
            modelProjectLog.LogContent = logcontent;
            modelProjectLog.ProjectId = model.ProjectInfoId;
            bllProjectLog.Add(modelProjectLog);
            #endregion
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
    }
}